package com.joy.openggl;

import java.nio.FloatBuffer;
import java.nio.ShortBuffer;

import javax.microedition.khronos.opengles.GL10;

import com.joy.openggl.util.BufferUtil;

public class Square {
	// Our vertices.  
	 private float vertices[] = {  
		 -1.0f,  1.0f, 0.0f,  // 0, Top Left  
		 -1.0f, -1.0f, 0.0f,  // 1, Bottom Left  
		  1.0f, -1.0f, 0.0f,  // 2, Bottom Right  
		  1.0f,  1.0f, 0.0f,  // 3, Top Right  
	 };  
	   
	 // The order we like to connect them.  
	 private short[] indices = { 0, 1, 2, 0, 2, 3 };  
	   
	 // Our vertex buffer.  
	 protected FloatBuffer vertexBuffer;  
	   
	 // Our index buffer.  
	 protected ShortBuffer indexBuffer;  
	   
	 public Square() {   
		 vertexBuffer = BufferUtil.floatBuffer(vertices);
		 indexBuffer = BufferUtil.shortBuffer(indices);  
	 }  
	   
	 /**  
	 * This function draws our square on screen.  
	 * @param gl  
	 */  
	 public void draw(GL10 gl) {  
		 // Counter-clockwise winding.  设置逆时针方法为面的“前面”：
		 gl.glFrontFace(GL10.GL_CCW);  
		 // Enable face culling.  打开 忽略“后面”设置：
		 gl.glEnable(GL10.GL_CULL_FACE);  
		 // What faces to remove with the face culling.  明确指明“忽略“哪个面的代码如下：
		 gl.glCullFace(GL10.GL_BACK);  
		   
		 // Enabled the vertices buffer for writing  
		 //and to be used during  
		 // rendering.  
		 gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);  
		 // Specifies the location and data format of  
		 //an array of vertex  
		 // coordinates to use when rendering.  
		 gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);  
		   
		 gl.glDrawElements(GL10.GL_TRIANGLES, indices.length, GL10.GL_UNSIGNED_SHORT, indexBuffer);  
		   
		 // Disable the vertices buffer.  
		 gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);  
		 // Disable face culling.  
		 gl.glDisable(GL10.GL_CULL_FACE);  
	 }  
}
